<template>
  <div class="mod-goods">
    <el-form :inline="true">
      <el-form-item>
        <el-button type="success" @click="addOrUpdateHandle()">新增</el-button>
      </el-form-item>
    </el-form>
    <el-table :data="dataList" row-key="id" border v-loading="dataListLoading" style="width: 100%;">
      <el-table-column prop="categoryName" header-align="center" align="left" label="分类名">
        <template slot-scope="scope">
          <span :style="getColor(scope.row)">{{scope.row.categoryName}}</span>
        </template>
      </el-table-column>
      <el-table-column prop="categoryIcon" header-align="center" align="center" label="图标">
        <template slot-scope="scope">
          <img :src="scope.row.categoryIcon" style="width: 40px; height: 40px;">
        </template>
      </el-table-column>
      <el-table-column prop="commonStatus" header-align="center" align="center" label="状态">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.commonStatus === 2" size="small" type="danger">封禁</el-tag>
          <el-tag v-else size="small">正常</el-tag>
        </template>
      </el-table-column>      
      <el-table-column prop="createTime" header-align="center" align="center" label="创建时间"></el-table-column>
      <el-table-column fixed="right" header-align="center" align="center" width="320" label="操作">
        <template slot-scope="scope">
          <el-button type="success" size="mini" @click="addOrUpdateHandle(scope.row.id)">编辑</el-button>
          <el-button type="danger" size="mini" @click="deleteHandle(scope.row.id)">删除</el-button>
          <el-button v-if="scope.row.categoryLevel !== 3" type="warning" size="mini" @click="addChildren(scope.row)">添加子类商品</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
    <!-- 弹窗, 新增子类 -->
    <add-children v-if="addChildrenVisible" ref="addChildren" @refreshDataList="getDataList"></add-children>
  </div>
</template>

<script>
  import AddOrUpdate from './category-add-or-update'
  import AddChildren from './category-add-children'
  export default {
    data () {
      return {
        dataList: [],
        dataListLoading: false,
        addOrUpdateVisible: false,
        addChildrenVisible: false
      }
    },
    activated () {
      this.getDataList()
    },
    components: {
      AddOrUpdate,
      AddChildren
    },
    methods: {
      // 获取数据列表
      getDataList () {
        this.dataListLoading = true
        this.$http({
          url: this.$http.adornUrl('/mall-wares/wares/category/listCategoryTree'),
          method: 'get'
        }).then(({data}) => {
          console.log(data)
          this.dataList = data.data
          this.dataListLoading = false
        })
      },
      // 编辑
      addOrUpdateHandle (id) {
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(id)
        })
      },
      // 新增子类商品
      addChildren (row) {
        this.addChildrenVisible = true
        this.$nextTick(() => {
          this.$refs.addChildren.init(row)
        })
      },
      // 删除
      deleteHandle (id) {
        this.$confirm(`是否进行${id ? '删除' : '批量删除'}?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$http({
            url: this.$http.adornUrl(`/mall-wares/wares/category/deleteCategory/${id}`),
            method: 'get'
          }).then(({data}) => {
            if (data.code === 200) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.getDataList()
                }
              })
            } else {
              this.$message({
                showClose: true,
                message: data.message,
                type: 'warning',
                center: true
              })
            }
          })
        }).catch(() => {})
      },
      getColor (row) {
        if (row.categoryLevel === 1) {
          return { 'color': 'tomato', 'font-size': '16px' }
        } else if (row.categoryLevel === 2) {
          return { 'color': 'blue', 'font-size': '15px' }
        } else {
          return { 'color': 'burlywood' }
        }
      }
    }
  }
</script>
